package com.example.blood_sugar.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.example.blood_sugar.R;

/**
 * Created by Administrator on 2017/2/28.
 * 使TextView的Drawable和Text可以更好的居中显示
 */
public class DrawableTextView extends android.support.v7.widget.AppCompatTextView {

    private Paint mBitPaint;

    private boolean isSelected = true;

    private int mTotalWidth, mTotalHeight;

    private Bitmap mBitmap;

    private Rect mSrcRect, mDestRect;

    public DrawableTextView(Context context) {
        this(context, null);
    }

    public DrawableTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.history_16)).getBitmap();
        mBitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        /*
        1)mPaint.setAntiAlias();
        2)mPaint.setFilterBitmap(true)。
        第一个函数是用来防止边缘的锯齿，
        第二个函数是用来对位图进行滤波处理
         */
        mBitPaint.setFilterBitmap(true);
        mBitPaint.setDither(true); //该方法是设置防抖动。???
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
        isSelected = typedArray.getBoolean(R.styleable.DrawableTextView_isSelected, false);
    }

    public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {/* getCompoundDrawables() : Returns drawables for the left, top, right, and bottom borders.*/
        Drawable[] drawables = getCompoundDrawables();/* 得到drawableLeft设置的drawable对象*/
        Drawable leftDrawable = drawables[0];
        if (leftDrawable != null) {/* 得到leftDrawable的宽度*/
            int leftDrawableWidth = leftDrawable.getIntrinsicWidth();/* 得到drawable与text之间的间距*/
            int drawablePadding = getCompoundDrawablePadding();/* 得到文本的宽度*/
            int textWidth = (int) getPaint().measureText(getText().toString().trim());
            int bodyWidth = leftDrawableWidth + drawablePadding + textWidth;
            canvas.save();
            canvas.translate((getWidth() - bodyWidth) / 2, 0);
        }

        if (isSelected) {
            int mBitWidth = mBitmap.getWidth();
            int mBitHeight = mBitmap.getHeight();
            mSrcRect = new Rect(0, 0, mBitWidth, mBitHeight);
            mDestRect = new Rect(mTotalWidth - mBitWidth, 0, mTotalWidth, mBitHeight);
            canvas.drawBitmap(mBitmap, mSrcRect, mDestRect, mBitPaint);
        }

        super.onDraw(canvas);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mTotalHeight = h;
    }

    @Override
    public void setSelected(boolean selected) {
        isSelected = selected;
        this.invalidate();
    }
}
